<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title">我的频道</div>
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
        >{{ isEdit ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <van-grid :gutter="4">
      <van-grid-item
        v-for="(channel, index) in myChannels"
        :key="index"
        :text="channel.name"
        @click="onMyChannelClick(channel, index)"
      >
        <van-icon
          slot="icon"
          name="clear"
          v-show="isEdit && !fiexdChannles.includes(channel.id)"
        ></van-icon>
        <span
          class="text"
          :class="{ active: index === activeIndex }"
          slot="text"
          >{{ channel.name }}</span
        >
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <div slot="title">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        v-for="channel in recommendChannele"
        :key="channel.id"
        :text="channel.name"
        @click="onAddChannel(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel
} from '@/api/channel.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'ChannelEdit',
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    activeIndex: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [], // 所有频道
      isEdit: false,
      fiexdChannles: [0]
    }
  },
  created () {
    this.loadAllChannels()
  },
  mounted () {},
  methods: {
    async loadAllChannels () {
      const { data } = await getAllChannels()
      this.allChannels = data.data.channels
    },
    async onAddChannel (channel) {
      this.myChannels.push(channel)
      if (this.user) {
        // 已登录
        try {
          await addUserChannel({
            id: channel.id,
            seq: this.myChannels.length
          })
        } catch (err) {
          this.$toast('保存失败，请稍后再试')
        }
      } else {
        // 未登录
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    onMyChannelClick (channel, index) {
      // 判断是否是固定频道,是就不能删除
      if (this.isEdit) {
        if (this.fiexdChannles.includes(channel.id)) {
          return
        }
        // 数组删除,计算属性会根据数据的变化从新计算
        this.myChannels.splice(index, 1)

        if (index <= this.activeIndex) {
          this.$emit('update-active', this.activeIndex - 1, true)
        }
        this.deleteChannel(channel)
      } else {
        console.log(channel, index)
        this.$emit('update-active', index, false)
      }
    }, // 分装一个删除频道功能
    async deleteChannel (channel) {
      try {
        if (this.user) {
          await deleteUserChannel(channel.id)
        } else {
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } catch (err) {
        this.$toast('操作失败，请稍后重试')
      }
    }
  },
  computed: {
    recommendChannele () {
      const channels = []
      this.allChannels.forEach(channel => {
        const ret = this.myChannels.find(userChannels => {
          return userChannels.id === channel.id
        })
        if (!ret) {
          channels.push(channel)
        }
      })
      return channels
    },
    ...mapState(['user'])
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
}
/deep/.text {
  font: 16px !important;
}
.active {
  color: red;
}
/deep/.van-grid-item__icon-wrapper {
  position: unset;
}
.van-icon.van-icon-clear {
  position: absolute;
  right: -10px;
  top: -11px;
  z-index: 2;
}
</style>
